<?php
/**
 * Created by PhpStorm.
 * User: 54946
 * Date: 2017/8/8
 * Time: 22:26
 */
namespace App\Repositories;

use App\Http\Model\Article;
use App\Http\Model\User;
use Illuminate\Support\Facades\Crypt;

class ArticleRepository {

    use BaseRepository;
    protected $model;

    /**
     * UserRepository constructor.
     * @param User $user
     */
    public function __construct(Article $article)
    {
        $this->model=$article;
    }

    /**
     * Get some record
     * @param $count
     * @return \Illuminate\Support\Collection
     */
    public function getSome($count)
    {
        return $this->model->orderByDesc('created_at')->take($count)->get();
    }



    /**
     * 返回满足条件的文章
     * @param $condition
     * @return \Illuminate\Support\Collection
     */
    public function getArticle($condition)
    {
        return $this->model->whereIn('status',$condition)->orderByDesc('created_at')->paginate(config('constants.PAGE_SIZE'));
    }

    /**
     * 返回当前用户的文章信息
     * @param $nowUser
     * @param $status
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */

    public function getArticleByUser($nowUser,$status)
    {
        if($status!='-1'){
            return $this->model->where('user_id',$nowUser)->where('status',$status)->paginate(config('constants.PAGE_SIZE'));
        }else{
            return $this->model->whereIn('status',['0','1','2'])->where('user_id',$nowUser)->paginate(config('constants.PAGE_SIZE'));
        }

    }


    /***
     *
     * 以下方法供前端页面使用
     * @param $status
     * @param $limit
     * @return \Illuminate\Support\Collection
     */

    public function limitGetRecordByStatus($category,$status,$limit,$user)
    {
        return $this->model->where('category',$category)->where('user_id',$user)->where('status',$status)->orderByDesc('created_at')->take($limit)->get(['id','avatar','title','created_at']);
    }


    /**
     * 热点资讯
     * @param $limit
     * @param $orderCondition
     * @param $category
     * @return \Illuminate\Support\Collection
     */
    public function getHot($limit,$orderCondition,$category)
    {
        return $this->model->where('status','1')->where('category',$category)->orderByDesc($orderCondition)->take($limit)->get(['id','avatar','title','created_at']);
    }

    /**
     * 浏览量递增
     * @param $id
     * @param int $value
     * @return int
     */
    public function incrementCount($id,$value=1)
    {
        return $this->model->where('id',$id)->increment('count',$value);
    }


    /**
     * 根据keyWord返回信息
     * @param $keyWord
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator\
     */
    public function searchReturn($keyWord)
    {
        return $this->model->where('title','like','%'.$keyWord.'%')->where('status','1')->select('id','title','created_at')->paginate(config('constants.PAGE_SIZE'));
    }

    /**
     * 根据类别返回更多
     * @param $category
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getMoreByCategory($category)
    {
        return $this->model->where('category',$category)->where('status','1')->orderByDesc('created_at')->select('id','title','created_at')->paginate(config('constants.PAGE_SIZE'));
    }

}